"use strict";

function ShaddowingPanel(){

}

function removeKeyFrame(){
	var p = $('#setRoot');
	if(p.BHasClass("AbilityUp")){
		p.SetHasClass("AbilityUp",false);
		$('#setRoot').style.y = "500px";
	}

	if(p.BHasClass("AbilityDown")){
		p.SetHasClass("AbilityDown",false);
		$('#setRoot').style.y = "0px";
	}
}
var jsonstr = "{\"npc_dota_hero_nevermore\":{\"youxia_tower11\":[\"youxia9_3\"],\"youxia_tower9\":[\"youxia5_3\"],\"youxia_tower7\":[\"youxia3_3\"],\"youxia_tower6\":[\"youxia2_3\"],\"youxia_tower8\":[\"youxia4_3\"],\"youxia_tower10\":[\"youxia6_3\"],\"youxia_tower5\":[\"youxia1_3\"]},\"npc_dota_hero_wisp\":{\"mojianshi_tower11\":[\"mojianshi9_3\"],\"mojianshi_tower10\":[\"mojianshi6_3\"],\"mojianshi_tower9\":[\"mojianshi5_3\"],\"mojianshi_tower8\":[\"mojianshi4_3\"],\"mojianshi_tower7\":[\"mojianshi3_3\"],\"mojianshi_tower5\":[\"mojianshi1_3\"],\"mojianshi_tower6\":[\"mojianshi2_3\"]},\"npc_dota_hero_rubick\":{\"xiaotou_tower11\":[\"xiaotou9_3\"],\"xiaotou_tower7\":[\"xiaotou3_3\"],\"xiaotou_tower10\":[\"xiaotou6_3\"],\"xiaotou_tower9\":[\"xiaotou5_3\"],\"xiaotou_tower8\":[\"xiaotou4_3\"],\"xiaotou_tower6\":[\"xiaotou2_3\"],\"xiaotou_tower5\":[\"xiaotou1_3\"]},\"npc_dota_hero_beastmaster\":{\"lieren_tower7\":[\"lieren3_3\"],\"lieren_tower6\":[\"lieren2_3\"],\"lieren_tower5\":[\"lieren1_3\"],\"lieren_tower8\":[\"lieren4_3\"],\"lieren_tower10\":[\"lieren6_3\"],\"lieren_tower9\":[\"lieren5_3\"],\"lieren_tower11\":[\"lieren9_3\"]},\"npc_dota_hero_doom_bringer\":{\"yemanren_tower5\":[\"yemanren1_3\"],\"yemanren_tower6\":[\"yemanren2_3\"],\"yemanren_tower9\":[\"yemanren5_3\"],\"yemanren_tower7\":[\"yemanren3_3\"],\"yemanren_tower11\":[\"yemanren9_3\"],\"yemanren_tower8\":[\"yemanren4_3\"],\"yemanren_tower10\":[\"yemanren6_3\"]},\"npc_dota_hero_puck\":{\"shushi_tower9\":[\"shushi5_3\"],\"shushi_tower5\":[\"shushi1_3\"],\"shushi_tower11\":[\"shushi9_3\"],\"shushi_tower6\":[\"shushi2_3\"],\"shushi_tower8\":[\"shushi4_3\"],\"shushi_tower10\":[\"shushi6_3\"],\"shushi_tower7\":[\"shushi3_3\"]}}";
var techTree = JSON.parse(jsonstr);

var m_AbilityPanels = [];
function UpdateAbilityList()
{
	// $('#ability_list1').RemoveAndDeleteChildren();
	// $('#ability_list2').RemoveAndDeleteChildren();
	// $('#ability_list3').RemoveAndDeleteChildren();
	// $('#ability_list4').RemoveAndDeleteChildren();
	var queryUnit = Players.GetLocalPlayerPortraitUnit();
	var playerId = Game.GetLocalPlayerID() ;
	var unitName = 	Entities.GetUnitName( queryUnit )

	// see if we can level up
	var nRemainingPoints = Entities.GetAbilityPoints( queryUnit );
	var bPointsToSpend = ( nRemainingPoints > 0 );
	var bControlsUnit = Entities.IsControllableByPlayer( queryUnit, playerId);

	var panelList = ["#ability_list1","#ability_list2","#ability_list3","#ability_list4"];
	var panelPerLine = 4;
	
	
	if( Entities.IsRealHero( queryUnit )){
		//如果是商店，就将技能列表放到屏幕中央，并且可以处理对其他单位的点击事件
		//如果单位距离商店过远，变黑,不放在update中，单独起一个事件
		// update all the panels
		$('#abilities').visible = false;
		$('#setRoot').visible = true;
		var nUsedPanels = 0;
		for ( var i = 0; i < Entities.GetAbilityCount( queryUnit ); ++i )
		{
			var ability = Entities.GetAbility( queryUnit, i );

			if ( ability == -1 )
				continue;
			var abilityListPanel = $('#ability_list'+(Math.floor(i/panelPerLine)+1));
			if ( !Abilities.IsDisplayedAbility(ability) )
				continue;
			if ( nUsedPanels >= m_AbilityPanels.length )
			{
				// create a new panel
				var abilityPanel = $.CreatePanel( "Panel", abilityListPanel, "" );
				abilityPanel.BLoadLayout( "file://{resources}/layout/custom_game/action_bar_ability.xml", false, false );
				m_AbilityPanels.push( abilityPanel );
			}
			// update the panel for the current unit / ability
			var abilityPanel = m_AbilityPanels[ nUsedPanels ];   
			var abname = Abilities.GetAbilityName( ability );
			abilityPanel.SetAbility( ability, queryUnit, techTree[unitName][abname] || [] );
			nUsedPanels++;
		} 
		for ( var i = nUsedPanels; i < m_AbilityPanels.length; ++i )
		{
			var abilityPanel = m_AbilityPanels[ i ];
			abilityPanel.SetAbility( -1, -1 ,[]);
		}
	}else {
		//如果不是我能控制的单位而且也不是商店，就显示为黑白
		$('#abilities').visible = true;
		$('#abilities').AddClass("abilitiesHero");
		$('#setRoot').visible = false;
		// for ( var i = 0; i < Entities.GetAbilityCount( queryUnit ); ++i )
		// {
		// 	var ability = Entities.GetAbility( queryUnit, i );
		// 	if ( ability == -1 )
		// 		continue;

		// 	if ( !Abilities.IsDisplayedAbility(ability) )
		// 		continue;

		// 	var abilityListPanel1 = $( "#ability_list1" );
		// 	var abilityListPanel2 = $( "#ability_list2" );
		// 	var abilityListPanel3 = $( "#ability_list3" );
		// 	abilityListPanel2.SetHasClass("Hidden", true);
		// 	abilityListPanel3.SetHasClass("Hidden", true);
		// 	if ( nUsedPanels >= m_AbilityPanels.length )
		// 	{
		// 		// create a new panel
		// 		var abilityPanel = $.CreatePanel( "Panel", abilityListPanel1, "" );
		// 		abilityPanel.BLoadLayout( "file://{resources}/layout/custom_game/action_bar_ability.xml", false, false );
		// 		m_AbilityPanels.push( abilityPanel );
		// 		abilityPanel.hiddenHotkey(true);
		// 	}

		// 	// update the panel for the current unit / ability
		// 	var abilityPanel = m_AbilityPanels[ nUsedPanels ];   

		// 	abilityPanel.SetAbility( ability, queryUnit, unitInfo );

		// 	nUsedPanels++;
		// }

	}

//	$.GetContextPanel().SetHasClass( "could_level_up", ( bControlsUnit && bPointsToSpend ) );

	// clear any remaining panels
}

(function()
{

	GameEvents.Subscribe( "dota_portrait_ability_layout_changed", UpdateAbilityList );
	GameEvents.Subscribe( "dota_player_update_selected_unit", UpdateAbilityList );
	GameEvents.Subscribe( "dota_player_update_query_unit", UpdateAbilityList );
	GameEvents.Subscribe( "dota_ability_changed", UpdateAbilityList );
	GameEvents.Subscribe( "dota_hero_ability_points_changed", UpdateAbilityList );
	
	UpdateAbilityList(); // initial update
})();

